package cn.codergege.demo.controller

import cn.codergege.demo.entity.primary.Person
import cn.codergege.demo.service.PersonService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class HelloController {

    @Autowired
    lateinit var personService: PersonService

    private val log = LoggerFactory.getLogger(javaClass)

    @GetMapping("/multi")
    fun multiDs(): Any {
        println("从 Primary 数据库中查询, 并插入 Secondary")
        personService.multiDsService()
        return "插入完成"
    }

    @GetMapping(*arrayOf("/hello", "/hi"))
    fun hello(): Any? {
        log.info("Hello world! ...")
        val persons = personService.findPersonsByGender(1)
        persons?.forEach(::println)
        return persons
    }

    @GetMapping("/person/{id}")
    fun getPerson(@PathVariable("id") id: Int): Person {
        return personService.findPersonById(id)
    }
}
